import axios from 'axios'
import { Modal } from 'view-design'
import store from '@/store'
axios.defaults.baseURL="/api"
axios.interceptors.request.use(config=>{
        config.headers.Authorization=store.state.user.token!=null
            ?store.state.user.token+"="+(new Date()).getTime()
            :(new Date()).getTime();
        return config
})

axios.interceptors.response.use(res=>{
    return res;
},error=>{
    switch (error.response.status) {
        case 403:
            Modal.error({
                title:"警告",
                content:"嘿嘿，你还权限哦"
            })
            break;
        case 500:
            let content = ""
            if(error.response.data.message=="token无效"){
                content = "登录失效，请重新登录"
            }else {
                content = "服务器出错了"
            }
            Modal.error({
                title:"警告",
                content:content,
                onOk(){
                    if(error.response.data.message=="token无效") {
                        store.commit("setToken","")
                        window.location.reload();
                    }
                }
            })
            break;
        case 404:
            Modal.error({
                title:"警告",
                content:"你的资源飞走了"
            })
            break;
        case 401:
            Modal.error({
                title:"警告",
                content:"认证已过期，请重新验证",
                onOk(){
                    store.commit("setToken","")
                    window.location.reload();
                }
            })
            break;
        default :
            Modal.error({
                title:"警告",
                content:"不知道啥错"
            })
    }
})

export default {
    /**
     * 获取资源
     * @param url
     * @param params
     * @returns {Promise<unknown>}
     */
    get(url, params) {
        return new Promise((resolve, reject) => {
            axios.get("/backstage"+url, params)
                .then(res => {
                    resolve(res);
                })
                .catch(err => {
                    reject(err)
                })
        });
    },
    /**
     * 添加资源或者获取资源
     * @param url
     * @param params
     * @returns {Promise<unknown>}
     */
    post(url,params){
        return new Promise((resolve, reject) => {
            axios.post("/backstage"+url, params)
                .then(res => {
                    resolve(res);
                })
                .catch(err => {
                    reject(err)
                })
        });
    },
    /**
     * 删除资源
     * @param url
     * @param params
     * @returns {Promise<unknown>}
     */
    delete(url, params){
        return new Promise((resolve, reject) =>{
            axios.delete("/backstage"+url, params)
                .then(res=>{
                    resolve(res)
                }).catch(err=>{
                reject(err)
            })
        })
    },
    /**
     * 更新资源
     * @param url
     * @param params
     * @returns {Promise<unknown>}
     */
    put(url, params){
        return new Promise((resolve, reject) =>{
            axios.put("/backstage"+url, params)
                .then(res=>{
                    resolve(res)
                }).catch(err=>{
                reject(err)
            })
        })
    },
    /**
     *登录、退出登录、修改密码
     * @param url
     * @param params
     * @returns {Promise<unknown>}
     */
    auth(url,params){
        return new Promise((resolve, reject) => {
            axios.post("/auth"+url, params)
                .then(res => {
                    resolve(res);
                })
                .catch(err => {
                    reject(err)
                })
        });
    },
    tget(url,params){
        return new Promise((resolve, reject) => {
            axios.get("/teacher"+url, params)
                .then(res => {
                    resolve(res);
                })
                .catch(err => {
                    reject(err)
                })
        });
    },
    tput(url, params){
        return new Promise((resolve, reject) =>{
            axios.put("/teacher"+url, params)
                .then(res=>{
                    resolve(res)
                }).catch(err=>{
                reject(err)
            })
        })
    },
    tpost(url,params){
        return new Promise((resolve, reject) => {
            axios.post("/teacher"+url, params)
                .then(res => {
                    resolve(res);
                })
                .catch(err => {
                    reject(err)
                })
        });
    },
    tdelete(url, params){
        return new Promise((resolve, reject) =>{
            axios.delete("/teacher"+url, params)
                .then(res=>{
                    resolve(res)
                }).catch(err=>{
                reject(err)
            })
        })
    },
}
